package widgets

import (
	"reflect"
	"strings"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/os/gfile"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/goki/ki/ki"
	"github.com/goki/ki/kit"
	octicons "github.com/lusingander/fyne-octicons"
	"github.com/y2h/fyne_poem/dbb"
	"github.com/y2h/fyne_poem/util"
)

// 显示数据库名、表名、列名
var Treee *widget.Tree

type YhNodeDB struct {
	ki.Node
}

var YhNodeDBProps = ki.Props{
	"intprop":    -17,
	"floatprop":  3.1415,
	"stringprop": "type string",
}
var KiT_YhNodeDB = kit.Types.AddType(&YhNodeDB{}, YhNodeDBProps)

var parent = YhNodeDB{}

func KiRoot() YhNodeDB {
	parent.InitName(&parent, "root")
	return parent
}
func SetKi(db gdb.DB, dbname string) {
	typ := reflect.TypeOf(parent)
	tbls, _ := db.Tables(db.GetCtx())
	dbnode := parent.AddNewChild(typ, dbname)
	dbnode.SetProp("numtbl", len(tbls))
	ktbls := "Tables(" + gconv.String(len(tbls)) + ")"
	tblnode := dbnode.AddNewChild(typ, ktbls)
	for _, tbl := range tbls {
		tblnode := tblnode.AddNewChild(typ, tbl)
		_, cols := dbb.SqliteTblColsGF(db, tbl)
		for i, col := range cols {
			tblnode.AddNewChild(typ, col)
			cols[i] = util.TblCol(tbl, col)
		}
	}
}
func Dbtree(db gdb.DB, msg *widget.Label,
	w fyne.Window, a fyne.App) *container.DocTabs {
	tbls, _ := db.Tables(db.GetCtx())
	tabs := container.NewDocTabs()
	var tree1 *widget.Tree
	var sel string = ""
	tree1 = widget.NewTree(func(tni widget.TreeNodeID) []widget.TreeNodeID {
		if tni == "" {
			res := make([]string, 0, 20)
			clds := *parent.Children()
			for _, cld := range clds {
				res = append(res, cld.Path())
			}
			return res
		}

		node := parent.FindPath(tni)
		res := make([]string, 0, 20)
		for _, cld := range *node.Children() {
			res = append(res, cld.Path())
		}
		// fmt.Printf("res1: %v\n", res)
		return res
	}, func(tni widget.TreeNodeID) bool {
		if tni == "" {
			return parent.HasChildren()
		}

		return parent.FindPath(tni).HasChildren()
	}, func(b bool) fyne.CanvasObject {
		return NewClickLbl("Template Object", nil, nil)
	}, func(tni widget.TreeNodeID, b bool, co fyne.CanvasObject) {
		lbl := co.(*Clicklbl)
		v, found := garray.NewStrArrayFrom(strings.Split(
			parent.FindPath(tni).Path(), "/")).PopRight()
		if found {
			lbl.SetText(v)
		}
		// lbl.SetText(tni)
		lbl.OnDoubleTapped = func() {
			Treee.Select(tni)
			if !util.IsInSlice(v, tbls) {
				return
			}
			dataBrowser := container.NewTabItemWithIcon("",
				octicons.NoteIcon(),
				Tbs(db, v, msg, w, a))

			dataBrowser.Text = v + "(" +
				gfile.Name(db.GetConfig().Name) + ")"
			tabs.Append(dataBrowser)
			tabs.Select(dataBrowser)
		}
		lbl.OnTapped = func() {
			tree1.Select(tni)
		}
	})
	bottom := widget.NewLabel("")
	tree1.OnSelected = func(uid widget.TreeNodeID) {
		sel = uid
		bottom.SetText(uid)
	}
	input := widget.NewEntry()
	input.SetText("aaaaaaaaaaaaaaaaaa")
	top := container.NewHBox(
		widget.NewButton("Add child", func() {
			typ := reflect.TypeOf(parent)
			parent.FindPath(sel).AddNewChild(typ,
				input.Text)
			ki.UniquifyNamesAll(parent.This())
			tree1.Refresh()
		}), widget.NewButton("delete child", func() {
			parent.FindPath(sel).Delete(true)
			tree1.Refresh()
		}))
	top = container.NewVBox(input, top)
	cc := container.NewBorder(top, bottom, nil, nil, tree1)
	dataBrowser := container.NewTabItemWithIcon("",
		octicons.NoteIcon(),
		cc)
	dataBrowser.Text = "haha"
	tabs.Append(dataBrowser)
	return tabs
	// return tree1
}
